Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Earth - Beauttie #11

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Earth - Beauttie #11

wants to merge 5 commits into from

Conversation

beauttie
Copy link

Heaps Practice

Congratulations! You're submitting your assignment!

Comprehension Questions

Question Answer
How is a Heap different from a Binary Search Tree? Nodes in a heap only have a relationship with its parent whereas nodes in a BST have a relationship with its parent and sibling (the value of the left node is less than that of its parent while the value of the right is greater than). Heaps also have to be almost complete, and the last level has to be filled from left to right.
Could you build a heap with linked nodes? You could, but accessing a parent/child node would approach linear time complexity. With arrays, we are able to reduce this given a mathematical formula for calculating the index of a node's parent/children using its index.
Why is adding a node to a heap an O(log n) operation? You are performing one swap per level in the heap (making a comparison between a node and its parent). This divides the number of swaps relative to the number of nodes to complete the operation in about half, which is O(log n) time complexity.
Were the heap_up & heap_down methods useful? Why? Yes, they were helper methods for adding and removing a node in a heap respectively.

Copy link

@CheezItMan CheezItMan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work Beauttie, you hit the learning goals here. Well done.

Comment on lines +4 to +7
# Time Complexity: O(n log n); adding n nodes to a heap is O(n log n),
# while removing n nodes from the heap is O(n log n) => O(nlog n + nlog n)
# Space Complexity: O(n) because of heap with up to n nodes
def heapsort(list)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 , very compact

Comment on lines +17 to 19
# Time Complexity: O(log n)
# Space Complexity: O(1)
def add(key, value = key)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Comment on lines +26 to 28
# Time Complexity: O(log n)
# Space Complexity: O(1)
def remove()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Comment on lines +53 to 55
# Time complexity: O(1)
# Space complexity: O(1)
def empty?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Comment on lines +64 to 66
# Time complexity: O(log n)
# Space complexity: O(1)
def heap_up(index)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Comment on lines 76 to 79
# This helper method takes an index and
# moves it up the heap if it's smaller
# than it's parent node.
def heap_down(index)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants